﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ToyundaPhone.FileFormat;

namespace FileFormatTest
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		public MainWindow()
		{
			InitializeComponent();
		}

		private void ChooseFile_Click(object sender, RoutedEventArgs e)
		{
			Microsoft.Win32.OpenFileDialog openDialog = new Microsoft.Win32.OpenFileDialog();
			openDialog.AddExtension = false;
			openDialog.CheckFileExists = true;
			openDialog.CheckPathExists = true;
			openDialog.DereferenceLinks = true;
			openDialog.Filter = "All files (*)|*";
			openDialog.FilterIndex = 0;
			openDialog.Multiselect = false;
			openDialog.ShowReadOnly = false;
			openDialog.RestoreDirectory = true;
			openDialog.ReadOnlyChecked = true;
			openDialog.Title = "Select a file to parse.";
			bool? result = openDialog.ShowDialog(this);
			if (!result.Value) return;
			filename.Text = openDialog.FileName;
			elements.Items.Clear();
			using (System.IO.TextReader reader = new System.IO.StreamReader(openDialog.FileName, Encoding.UTF8))
			{
				GenericDelimitedFileLexer lexer = new DashDelimitedFileLexer(reader);
				foreach (var row in new ToyundaPhone.Helpers.NameCleaningRowFilter(new FixedTextColumnCountFilter(new DelimitedFileParser(lexer), 3, new string(lexer.Delimiter))).AsEnumerable())
				{
					var item = new ListViewItem();
					elements.Items.Add(new {
							Type = row[0],
							Work = row[1],
							Title = (row[2] != null) ? row[2] : string.Empty
						});
				}
			}
		}
	}
}
